package com.yfq.gateway.apigatewaysdk.application;

import com.yfq.gateway.apigatewaysdk.annotation.ApiProducerInterface;
import com.yfq.gateway.apigatewaysdk.annotation.ApiProducerMethod;
import com.yfq.gateway.apigatewaysdk.config.GatewaySdkProperties;
import com.yfq.gateway.apigatewaysdk.domain.service.GatewayCenterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.lang.reflect.Method;

/**
 * @author yfq
 * @date 2024/1/5 14:13
 * @description 每个bean初始化完成后进行接口注册
 */
public class GatewaySdkInterfaceRegister implements BeanPostProcessor {
    private final Logger logger = LoggerFactory.getLogger(GatewaySdkInterfaceRegister.class);

    private final GatewaySdkProperties properties;
    private final GatewayCenterService gatewayCenterService;

    public GatewaySdkInterfaceRegister(GatewaySdkProperties gatewaySdkProperties, GatewayCenterService gatewayCenterService) {
        this.properties = gatewaySdkProperties;
        this.gatewayCenterService = gatewayCenterService;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        ApiProducerInterface apiProducerInterface = bean.getClass().getAnnotation(ApiProducerInterface.class);
        if (apiProducerInterface == null) {
            return bean;
        }

        //1.系统信息
        logger.info("注册接口系统信息：systemId：{}，systemName：{}", properties.getSystemId(), properties.getSystemName());
        gatewayCenterService.doRegistryApplication(properties.getSystemId(), properties.getSystemName(), "RPC", properties.getSystemRegistry());

        //2.接口信息
        String interfaceName = apiProducerInterface.interfaceName();
        String interfaceVersion = apiProducerInterface.interfaceVersion();
        logger.info("注册接口信息：interfaceName：{}，interfaceVersion：{}", interfaceName, interfaceVersion);
        Class<?>[] interfaces = bean.getClass().getInterfaces();
        String interfaceId = interfaces[0].getName();
        gatewayCenterService.doRegistryInterface(properties.getSystemId(), interfaceId, interfaceName, interfaceVersion);

        //3.方法信息
        Method[] methods = bean.getClass().getDeclaredMethods();
        for (Method method : methods) {
            ApiProducerMethod apiProducerMethod = method.getAnnotation(ApiProducerMethod.class);
            if (apiProducerMethod != null) {
                String methodName = apiProducerMethod.methodName();
                String uri = apiProducerMethod.uri();
                String httpStatementType = apiProducerMethod.httpStatementType();
                int auth = apiProducerMethod.auth();
                logger.info("注册接口方法信息：methodName：{}，uri：{}，httpStatementType：{}，auth：{}", methodName, uri, httpStatementType, auth);
                String methodId = method.getName();
                Class<?>[] parameterTypes = method.getParameterTypes();
                String parameterType = parameterTypes[0].getName();
                gatewayCenterService.doRegistryMethod(properties.getSystemId(), interfaceId, methodId, methodName, parameterType, uri, httpStatementType, auth);
            }
        }

        return bean;
    }
}
